﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web.Handlers
{
    /// <summary>
    /// GetFileForCpp 的摘要说明
    /// c++根据传参获取文件数据
    /// </summary>
    public class GetFileForCpp : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write(GetFileDataForCpp(context));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string GetFileDataForCpp(HttpContext context)
        {
            try
            {
                List<byte[]> result = new List<byte[]>();
                string db = context.Request["db"];
                string areaCode = context.Request["areaCode"];
                string kindId = context.Request["kindId"];
                string sysCode = "EF";
                string funcCode = GetFuncCode(kindId);
                string objectId = context.Request["objectId"];
                if (string.IsNullOrEmpty(db) || string.IsNullOrEmpty(kindId) || string.IsNullOrEmpty(objectId))
                {
                    return string.Empty;
                }
                objectId = GetObjectId(kindId, objectId, areaCode, db);
                if ((!string.IsNullOrEmpty(objectId)) && (!string.IsNullOrEmpty(sysCode)) && (!string.IsNullOrEmpty(funcCode)))
                {
                    using (FileService.FileWebServiceSoapClient client = new FileService.FileWebServiceSoapClient())
                    {
                        FileService.ArrayOfString files = client.GetFileList(objectId, sysCode, funcCode);
                        foreach (var fileName in files)
                        {
                            var fileData = client.DownFirstImageBytes(fileName, sysCode, funcCode);
                            result.Add(fileData);
                        }
                    }
                }
                return JsonConvert.SerializeObject(result);
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 根据类型获取FuncCode
        /// 企业资质	10
        /// 项目经理资质	20
        /// 企业安全生产许可证	11
        /// 企业法人营业执照	104
        /// 外地入青信用证明	102
        /// </summary>
        /// <param name="kindId"></param>
        /// <returns></returns>
        private string GetFuncCode(string kindId)
        {
            switch (kindId)
            {
                case "10":
                    return "CorpZZ";
                case "11":
                    return "CorpAQ";
                case "104":
                    return "Corp";
                case "102":
                    return "CorpWDZR";
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// 获取对象唯一标识
        /// 外地准入获取图片应使用分支机构的Guid，因此需要根据地区和corpUserGuid查询分支机构Guid
        /// 若数据库为青岛库，则青岛库没有上传任何图片，此时返回一个特殊的文件名以用于显示暂无图片图片
        /// </summary>
        /// <param name="kindId"></param>
        /// <param name="objectId"></param>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        private string GetObjectId(string kindId, string objectId, string areaCode,string db)
        {
            if (db.StartsWith("02") && db != "0209")
            {
                return "qddemo";
            }
            areaCode = areaCode.Length > 2 ? areaCode.Substring(0, 2) : areaCode;
            switch (kindId)
            {
                case "102":
                    Guid corpUserGuid = Guid.Empty;
                    if (Guid.TryParse(objectId, out corpUserGuid))
                    {
                        Tb_Corp_CompanyInfo corpInfo = new Tb_Corp_CompanyInfo();
                        corpInfo.CorpUserGuid = corpUserGuid;
                        EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
                        var embrabchMent = JsonConvert.DeserializeObject<List<Tb_Corp_Embranchment>>(enterpriseServiceClient.GetCorpEmbranchments(JsonConvert.SerializeObject(corpInfo)));
                        var currentEmbranchment = embrabchMent.Where(e => e.AreaCode.StartsWith(areaCode)).FirstOrDefault();
                        return currentEmbranchment == null ? string.Empty : currentEmbranchment.BranchGid.ToString();
                    }
                    return string.Empty;
                default:
                    return objectId;
            }
        }
    }
}